package com.graduationproject.examonline.exam.service;

import com.alibaba.fastjson.JSONObject;
import com.graduationproject.examonline.exam.entity.Exam;
import com.graduationproject.examonline.exam.mapper.ExamMapper;
import com.graduationproject.examonline.examPaper.entity.ExamPaper;
import com.graduationproject.examonline.student.entity.Student;
import com.graduationproject.examonline.student.mapper.StudentMapper;
import com.graduationproject.examonline.utils.Result;
import com.graduationproject.examonline.webSocket.server.WebSocketServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class ExamService {

    @Autowired
    private ExamMapper examMapper;
    @Autowired
    private StudentMapper studentMapper;

    public Exam updateStatus(Exam exam) {
        try {
            String status = exam.getExam_status();
            // onging进行中，notStart未开始，over已结束，cancel已取消
            if (!status.equals("cancel")) {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                Date startTime = sdf.parse(exam.getStart_time());
                Date endTime = sdf.parse(exam.getEnd_time());
                Date current = new Date();
                if (current.getTime() < startTime.getTime()) {
                    status = "notStart";
                } else if (current.getTime() < endTime.getTime()) {
                    status = "ongoing";
                } else if (current.getTime() > endTime.getTime()) {
                    status = "over";
                }
            }
            Map<String, Object> params = new HashMap<String, Object>();
            params.put("exam_id", exam.getExam_id());
            params.put("exam_status", status);
            examMapper.updateExamById(params);
            exam.setExam_status(status);
            return exam;
        } catch (Exception e) {
            e.printStackTrace();
            return exam;
        }
    }

    public Result getExamListByTeachId(String teach_id) {
        try {
            List<Exam> examList = examMapper.getExamListByTeachId(teach_id);
            List<Exam> nextExamList = new LinkedList<Exam>();
            for (int index = 0; index < examList.size(); index += 1) {
                Exam exam = examList.get(index);
                exam = this.updateStatus(exam);
                nextExamList.add(exam);
            }
            return new Result(0, 200, "查询成功", nextExamList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(1, 500, "查询失败");
        }
    }

    public Result insertExam(Map<String, Object> params) {
        try {
            int amount = examMapper.insertExam(params);
            if (amount > 0) {
                return new Result(0, 200, "插入成功");
            } else {
                return new Result(1, 200, "插入失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(1, 500, "插入失败");
        }
    }

    public Result updateExamByExamId(Map<String, Object> params) {
        try {
            System.out.println(params);
            int updateAmount = examMapper.updateExamById(params);
            if (updateAmount > 0) {
                Map<String, Object> message = new HashMap<String, Object>();
                List<Map<String, Object>> list1 = new ArrayList<Map<String, Object>>();
                list1.add(params);
                message.put("examListStatus", list1);
                WebSocketServer.sendMessageToAll(message);
            }
            return new Result(0, 200, "ok");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(1, 500, "error");
        }
    }

    public Result getExamListByStuId(String stu_id) {
        try {
            List<Student> students = studentMapper.findStudentById(stu_id);
            if (students.size() > 0) {
                Student student = students.get(0);
                String[] class_id = student.getClass_id().split(",");
                List<Exam> examList = new LinkedList<Exam>();
                for (int i = 0; i < class_id.length; i++) {
                    List<Exam> examList1 = examMapper.getExamListByClassId(class_id[i]);
                    for (int j = 0; j < examList1.size(); j++) {
                        examList.add(examList1.get(j));
                    }
                }
                List<Exam> nextExamList = new LinkedList<Exam>();
                for (int index = 0; index < examList.size(); index += 1) {
                    Exam exam = examList.get(index);
                    exam = this.updateStatus(exam);
                    nextExamList.add(exam);
                }
                return new Result(0, 200, "ok", nextExamList);
            }
            return new Result(1, 200, "nothing");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(1, 500, "error");
        }
    }

    public Result getStudentByExamId(String exam_id) {
        try {
            List<Exam> examList = examMapper.getExamListById(exam_id);
            List<Student> studentList = new ArrayList<Student>();
            if (examList.size() > 0) {
                String classIdListStr = examList.get(0).getClass_id();
                String[] classIdList = classIdListStr.split(",");
                for (int i = 0; i < classIdList.length; i++) {
                    String classId = classIdList[i];
                    List<Student> studentList2 = studentMapper.findStudentByClassId(classId);
                    studentList.addAll(studentList2);
                }
            }
            return new Result(0, 200, "ok", studentList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(1, 500, "error");
        }
    }
}
